#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
using namespace std;

const int maxn = 30;
vector<int> pile[maxn];
int n, a, b, pa, pb, ha, hb;
string s1, s2;

void find_block(int, int&, int&);
void clear_above(int, int);
void pile_onto(int, int, int);
void print();

int main() {
  cin >> n;
  int i;
  for (i = 0; i < n; i++) {
    pile[i].push_back(i);
  }

  while (cin >> s1 >> a >> s2 >> b) {
    if (a == b) {
      continue;
    }
    
    find_block(a, pa, ha);
    find_block(b, pb, hb);
    if (pa == pb) {
      continue;
    }

    if (s2 == "onto") {
      clear_above(pb, hb);
    }
    if (s1 == "move") {
      clear_above(pa, ha);
    }

    pile_onto(pa, ha, pb);
  }

  print();

  return 0;
}

void find_block(int x, int& p, int& h) {
  for (p = 0; p < n; p++) {
    int size = pile[p].size();
    for (h = 0; h < size; h++) {
      if (pile[p][h] == x) {
        return;
      }
    }
  }
}

void clear_above(int p, int h) {
  int i, size = pile[p].size();
  for (i = h + 1; i < size; i++) {
    int x = pile[p][i];
    pile[x].push_back(x);
  }
  pile[p].resize(h + 1);
}

void pile_onto(int p, int h, int destp) {
  int i, size = pile[p].size();
  for (i = h; i < size; i++) {
    pile[destp].push_back(pile[p][i]);
  }
  pile[p].resize(h);
}

void print() {
  int i, j;
  for (i = 0; i < n; i++) {
    printf("%d:", i);

    int size = pile[i].size();
    for (j = 0; j < size; j++) {
      printf(" %d", pile[i][j]);
    }

    printf("\n");
  }
}
